/**
 * 
 * 起迪科技 Copyright (c) 2014-2018 QiDi,Inc.All Rights Reserved.
 */

package cn.qidisoft.edu.hzjt.controller.bjxxclassworkInstead;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.common.pagination.Page;
import cn.qidisoft.core.common.pagination.PaginationBean;
import cn.qidisoft.core.util.moffice.excel.ExpExcelUtil;
import cn.qidisoft.edu.hzjt.annotation.SysLogger;
import cn.qidisoft.edu.hzjt.constants.Constant;
import cn.qidisoft.edu.hzjt.controller.BaseController;
import cn.qidisoft.edu.hzjt.exception.RPCException;
import cn.qidisoft.edu.hzjt.model.BjxxClassWorkInstead;
import cn.qidisoft.edu.hzjt.model.BjxxClassWorkInsteadAtt;
import cn.qidisoft.edu.hzjt.model.Employee;
import cn.qidisoft.edu.hzjt.model.ResponseResult;
import cn.qidisoft.edu.hzjt.model.Student;
import cn.qidisoft.edu.hzjt.model.UserEmployee;
import cn.qidisoft.edu.hzjt.service.IBjxxClassWorkInsteadAttService;
import cn.qidisoft.edu.hzjt.service.IBjxxClassWorkInsteadService;
import cn.qidisoft.edu.hzjt.service.ICommonService;
import cn.qidisoft.edu.hzjt.utils.LogType;

/**
 * 
 * @author hhl
 * @version $Id: ProjectController.java, v 0.1 2018年10月23日 下午4:56:53 hhl Exp $
 */

@Controller
@RequestMapping(value = "bjxxClassWorkInstead")
public class BjxxClassWorkInsteadController {
    protected static final LoggerAdapter    LOGGER = LoggerAdapterFactory
        .getLogger(LogType.HZJT_LOGGER.val);
    @Autowired
    private IBjxxClassWorkInsteadService    bjxxClassWorkInsteadService;

    @Autowired
    private IBjxxClassWorkInsteadAttService bjxxClassWorkInsteadAttService;

    @Autowired
    private ICommonService                  commonService;

    @RequestMapping(value = "/index_bzr")
    public String index(ModelMap model, HttpServletRequest request) {
        request.setAttribute("msg", BaseController.getParameter("msg"));
        BaseController.removeParameter("msg");
        return "bjxxClassWorkInstead/BjxxClassWorkInstead_List_bzr";
    }

    @RequestMapping(value = "/List")
    @ResponseBody
    public ResponseResult List(ModelMap model, HttpServletRequest request,
                               @RequestParam(value = "rows", required = false) String pageSize,
                               @RequestParam(value = "page", required = false) String currentPage,
                               BjxxClassWorkInstead data) {

        UserEmployee userEmployee = (UserEmployee) request.getSession()
            .getAttribute(Constant.LOGIN_CONSOLE_USER);
        try {
            Page page = new Page();
            page.setCurrPage(currentPage == null ? 0 : Integer.valueOf(currentPage));
            page.setPageSize(pageSize == null ? Constant.PageSize : Integer.valueOf(pageSize));
            BaseController.setSortByColumn(request, "applydate", "desc");
            data.setOrderField((String) request.getAttribute("orderField"));
            data.setOrderFlag((String) request.getAttribute("orderFlag"));
            data.setEmpid(userEmployee.getEmid());

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if (data.getApplydate1() == null || data.getApplydate1().equals("")) {

            } else {
                data.setBeginTime(sdf.parse(data.getApplydate1()));
            }
            if (data.getApplydate2() == null || data.getApplydate2().equals("")) {

            } else {
                data.setEndTime(sdf.parse(data.getApplydate2()));
            }

            PaginationBean<BjxxClassWorkInstead> paginationBean = bjxxClassWorkInsteadService
                .queryPageList(data, page);
            ResponseResult result = new ResponseResult();

            if (paginationBean.getPageList().isEmpty()) {
                result.setRows(new ArrayList<BjxxClassWorkInstead>());
            } else {
                result.setRows(paginationBean.getPageList());
            }
            result.setTotal(paginationBean.getTotalRecords());

            return result;

        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        return null;
    }

    @RequestMapping(value = "/addInInt", method = RequestMethod.GET)
    public String add(ModelMap model, HttpServletRequest request) {

        UserEmployee userEmployee = (UserEmployee) request.getSession()
            .getAttribute(Constant.LOGIN_CONSOLE_USER);
        /*
         * Employee employee = new Employee(); if (userEmployee.getEmid() != null) { Integer emid[] = {
         * userEmployee.getEmid() }; employee.setEmpids(emid); }
         */
        model.addAttribute("emid", userEmployee.getEmid());
        model.addAttribute("xm", userEmployee.getXm());
        model.addAttribute("zgh", userEmployee.getZgh());
        model.addAttribute("applydate", new Date());

        java.util.List<Employee> list = commonService.getqueryStudentClassByBzr(new Student());
        model.addAttribute("List", list);
        model.put("studentListJson", JSON.toJSONString(list));
        return "bjxxClassWorkInstead/add_BjxxClassWorkInstead";
    }

    @SysLogger(description = "班主任顶班信息", moduleName = "班主任顶班管理", method = "新增")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(ModelMap model, HttpServletRequest request, BjxxClassWorkInstead data) {
        try {

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if (data.getApplydate1().equals("")) { // 申请日期
                data.setApplydate(new Date());
            } else {
                data.setApplydate(sdf.parse(data.getApplydate1()));
            }
            data.setKsrq(sdf.parse(data.getKsrq1()));
            data.setJsrq(sdf.parse(data.getJsrq1()));

            Calendar cal = Calendar.getInstance();
            cal.setTime(data.getKsrq());
            long time1 = cal.getTimeInMillis();
            cal.setTime(data.getJsrq());
            long time2 = cal.getTimeInMillis();
            long between_days = (time2 - time1) / (1000 * 3600 * 24);
            if (data.getKsrqfw().equals("上午") && data.getJsrqfw().equals("上午")) {
                between_days = (long) (between_days + 0.5);
            } else if (data.getKsrqfw().equals("下午") && data.getJsrqfw().equals("下午")) {
                between_days = (long) (between_days + 0.5);
            } else if (data.getKsrqfw().equals("上午") && data.getJsrqfw().equals("下午")) {
                between_days = (long) (between_days + 1);
            } else if (between_days >= 1 && data.getKsrqfw().equals("下午")
                       && data.getJsrqfw().equals("上午")) {
                between_days = (long) (between_days + 1);
            }
            /*
             * System.out.println(Integer.parseInt(String.valueOf(between_days)));
             * System.out.println(BigDecimal.valueOf(between_days));
             */
            data.setDays(BigDecimal.valueOf(between_days));
            data.setDbxm(null);
            String gid = bjxxClassWorkInsteadService.add(data);

            BjxxClassWorkInstead data1 = new BjxxClassWorkInstead();
            data1.setGid(gid);
            data1 = bjxxClassWorkInsteadService.queryByPK(data1);
            String dbempids = request.getParameter("dbempids");
            if (!dbempids.equals("")) {
                for (String dd : dbempids.split("、")) {
                    BjxxClassWorkInsteadAtt att = new BjxxClassWorkInsteadAtt();
                    String[] strs = dd.split(",");
                    for (int i = 0; i < strs.length; i++) {
                        if (i == 0) {
                            att.setXm(strs[i].substring(1, strs[i].length()));
                        }
                        if (i == 1) {
                            att.setZgh(strs[i]);
                        }
                        if (i == 2) {
                            att.setEmpid(
                                Integer.valueOf(strs[i].substring(0, strs[i].length() - 1)));
                        }
                    }
                    att.setParentid(data1.getDbid());
                    bjxxClassWorkInsteadAttService.add(att);
                }
            }

            model.addAttribute("gid", gid);
            return "redirect:details";
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("添加 add [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(), e.getMessage());
            model.addAttribute(Constant.EXPTION_MSG_KEY, e);
            return "error/error";
        }
    }

    @SysLogger(description = "班主任顶班信息", moduleName = "班主任顶班管理", method = "新增")
    @RequestMapping(value = "/addSaveContinue", method = RequestMethod.POST)
    public String addSaveContinue(ModelMap model, HttpServletRequest request,
                                  BjxxClassWorkInstead data) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if (data.getApplydate1().equals("")) { // 申请日期
                data.setApplydate(new Date());
            } else {
                data.setApplydate(sdf.parse(data.getApplydate1()));
            }
            data.setKsrq(sdf.parse(data.getKsrq1()));
            data.setJsrq(sdf.parse(data.getJsrq1()));

            Calendar cal = Calendar.getInstance();
            cal.setTime(data.getKsrq());
            long time1 = cal.getTimeInMillis();
            cal.setTime(data.getJsrq());
            long time2 = cal.getTimeInMillis();
            long between_days = (time2 - time1) / (1000 * 3600 * 24);
            if (data.getKsrqfw().equals("上午") && data.getJsrqfw().equals("上午")) {
                between_days = (long) (between_days + 0.5);
            } else if (data.getKsrqfw().equals("下午") && data.getJsrqfw().equals("下午")) {
                between_days = (long) (between_days + 0.5);
            } else if (data.getKsrqfw().equals("上午") && data.getJsrqfw().equals("下午")) {
                between_days = (long) (between_days + 1);
            } else if (between_days >= 1 && data.getKsrqfw().equals("下午")
                       && data.getJsrqfw().equals("上午")) {
                between_days = (long) (between_days + 1);
            }

            /*
             * System.out.println(Integer.parseInt(String.valueOf(between_days)));
             * System.out.println(BigDecimal.valueOf(between_days));
             */
            data.setDays(BigDecimal.valueOf(between_days));
            data.setDbxm(null);
            String gid = bjxxClassWorkInsteadService.add(data);

            BjxxClassWorkInstead data1 = new BjxxClassWorkInstead();
            data1.setGid(gid);
            data1 = bjxxClassWorkInsteadService.queryByPK(data1);
            String dbempids = request.getParameter("dbempids");
            if (!dbempids.equals("")) {
                for (String dd : dbempids.split("、")) {
                    BjxxClassWorkInsteadAtt att = new BjxxClassWorkInsteadAtt();
                    String[] strs = dd.split(",");
                    for (int i = 0; i < strs.length; i++) {
                        if (i == 0) {
                            att.setXm(strs[i].substring(1, strs[i].length()));
                        }
                        if (i == 1) {
                            att.setZgh(strs[i]);
                        }
                        if (i == 2) {
                            att.setEmpid(
                                Integer.valueOf(strs[i].substring(0, strs[i].length() - 1)));
                        }
                    }
                    att.setParentid(data1.getDbid());
                    bjxxClassWorkInsteadAttService.add(att);
                }
            }
            return "redirect:addInInt";
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("添加项目信息 add [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(), e.getMessage());
            model.addAttribute(Constant.EXPTION_MSG_KEY, e);
            return "error/error";
        }
    }

    @RequestMapping(value = "/details", method = RequestMethod.GET)
    public String details(ModelMap model, HttpServletRequest request,
                          @RequestParam(value = "id", required = false) Integer id,
                          @RequestParam(value = "gid", required = false) String gid) {
        try {

            BjxxClassWorkInstead data = new BjxxClassWorkInstead();
            data.setDbid(id);
            data.setGid(gid);
            data = bjxxClassWorkInsteadService.queryByPK(data);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            data.setKsrq1(sdf.format(data.getKsrq()));
            data.setJsrq1(sdf.format(data.getJsrq()));
            data.setApplydate1(sdf.format(data.getApplydate()));
            model.addAttribute("data", data);
            return "bjxxClassWorkInstead/detailsBjxxClassWorkInstead_bzr";
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("查询 queryByPK [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(), e.getMessage());
            model.addAttribute(Constant.EXPTION_MSG_KEY, e);
            return "error/error";
        }
    }

    @RequestMapping(value = "/updateToview", method = RequestMethod.GET)
    public String updateToview(ModelMap model, HttpServletRequest request,
                               @RequestParam(value = "id") Integer id) {
        try {

            BjxxClassWorkInstead data = new BjxxClassWorkInstead();
            data.setDbid(id);
            data = bjxxClassWorkInsteadService.queryByPK(data);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            data.setKsrq1(sdf.format(data.getKsrq()));
            data.setJsrq1(sdf.format(data.getJsrq()));
            data.setApplydate1(sdf.format(data.getApplydate()));
            model.addAttribute("data", data);

            java.util.List<Employee> list = commonService.getqueryStudentClassByBzr(new Student());
            model.addAttribute("List", list);
            model.put("studentListJson", JSON.toJSONString(list));

            return "bjxxClassWorkInstead/updateBjxxClassWorkInstead_bzr";
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("查询 queryByPK [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(), e.getMessage());
            model.addAttribute(Constant.EXPTION_MSG_KEY, e);
            return "error/error";
        }
    }

    @SysLogger(description = "班主任顶班信息", moduleName = "班主任顶班管理", method = "修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(ModelMap model, HttpServletRequest request, BjxxClassWorkInstead data) {
        try {

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if (data.getApplydate1().equals("")) { // 申请日期
                data.setApplydate(new Date());
            } else {
                data.setApplydate(sdf.parse(data.getApplydate1()));
            }
            data.setKsrq(sdf.parse(data.getKsrq1()));
            data.setJsrq(sdf.parse(data.getJsrq1()));

            Calendar cal = Calendar.getInstance();
            cal.setTime(data.getKsrq());
            long time1 = cal.getTimeInMillis();
            cal.setTime(data.getJsrq());
            long time2 = cal.getTimeInMillis();
            long between_days = (time2 - time1) / (1000 * 3600 * 24);
            if (data.getKsrqfw().equals("上午") && data.getJsrqfw().equals("上午")) {
                between_days = (long) (between_days + 0.5);
            } else if (data.getKsrqfw().equals("下午") && data.getJsrqfw().equals("下午")) {
                between_days = (long) (between_days + 0.5);
            } else if (data.getKsrqfw().equals("上午") && data.getJsrqfw().equals("下午")) {
                between_days = (long) (between_days + 1);
            } else if (between_days >= 1 && data.getKsrqfw().equals("下午")
                       && data.getJsrqfw().equals("上午")) {
                between_days = (long) (between_days + 1);
            }
            data.setDays(BigDecimal.valueOf(between_days));
            data.setDbxm(null);
            bjxxClassWorkInsteadService.update(data);

            String dbempids = request.getParameter("dbempids");
            if (!dbempids.equals("")) {
                BjxxClassWorkInsteadAtt bjxxClassWorkInsteadAtt = new BjxxClassWorkInsteadAtt();
                bjxxClassWorkInsteadAtt.setParentid(data.getDbid());
                bjxxClassWorkInsteadAttService.delete(bjxxClassWorkInsteadAtt);
                for (String dd : dbempids.split("、")) {
                    BjxxClassWorkInsteadAtt att = new BjxxClassWorkInsteadAtt();
                    String[] strs = dd.split(",");
                    for (int i = 0; i < strs.length; i++) {
                        if (i == 0) {
                            att.setXm(strs[i].substring(1, strs[i].length()));
                        }
                        if (i == 1) {
                            att.setZgh(strs[i]);
                        }
                        if (i == 2) {
                            att.setEmpid(
                                Integer.valueOf(strs[i].substring(0, strs[i].length() - 1)));
                        }
                    }
                    att.setParentid(data.getDbid());
                    bjxxClassWorkInsteadAttService.add(att);
                }
            }

            BaseController.setParameter("msg", "保存成功");
            return "redirect:index_bzr";
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("查询 update [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(), e.getMessage());
            model.addAttribute(Constant.EXPTION_MSG_KEY, e);
            return "error/error";
        }
    }

    @SysLogger(description = "班主任顶班信息", moduleName = "班主任顶班管理", method = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String delete(ModelMap model, HttpServletRequest request) {
        try {
            String[] idList = request.getParameter("idList").split(",");
            BjxxClassWorkInstead data = new BjxxClassWorkInstead();
            data.setIds(idList);
            bjxxClassWorkInsteadService.delete(data);
            BaseController.setParameter("msg", "删除成功");
            return "redirect:index_bzr";
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("删除 delete [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(), e.getMessage());
            model.addAttribute(Constant.EXPTION_MSG_KEY, e);
            return "error/error";
        }
    }

    @SysLogger(description = "顶班管理信息", moduleName = "顶班管理", method = "提交")
    @RequestMapping(value = "/btnSubmit")
    public String btnSubmit(ModelMap model, HttpServletRequest request,
                            @RequestParam(value = "id", required = false) Integer id) {
        try {

            BjxxClassWorkInstead data = new BjxxClassWorkInstead();
            data.setDbid(id);
            data = bjxxClassWorkInsteadService.queryByPK(data);
            data.setStates(1);
            data.setAuditresult(null);
            data.setAuditorid(null);
            data.setAuditorname(null);
            data.setAudittime(null);
            bjxxClassWorkInsteadService.update(data);
            model.addAttribute("id", id);
            return "redirect:details";
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            model.addAttribute(Constant.EXPTION_MSG_KEY, e);
            return "error/error";
        }
    }

    @RequestMapping(value = "/index_admin")
    public String index_admin(ModelMap model, HttpServletRequest request) {
        request.setAttribute("msg", BaseController.getParameter("msg"));
        model.addAttribute("states", 1);
        BaseController.removeParameter("msg");
        return "bjxxClassWorkInstead/BjxxClassWorkInstead_List";
    }

    @RequestMapping(value = "/List_admin")
    @ResponseBody
    public ResponseResult List_admin(ModelMap model, HttpServletRequest request,
                                     @RequestParam(value = "rows", required = false) String pageSize,
                                     @RequestParam(value = "page", required = false) String currentPage,
                                     BjxxClassWorkInstead data) {
        try {

            Page page = new Page();
            page.setCurrPage(currentPage == null ? 0 : Integer.valueOf(currentPage));
            page.setPageSize(pageSize == null ? Constant.PageSize : Integer.valueOf(pageSize));
            BaseController.setSortByColumn(request, "applydate", "desc");
            data.setOrderField((String) request.getAttribute("orderField"));
            data.setOrderFlag((String) request.getAttribute("orderFlag"));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if (data.getStates() == null || data.getStates().equals("")) {
                data.setStates(1);
            } else if (data.getStates() == -1) {
                data.setStates(null);
            }

            if (data.getApplydate1() == null || data.getApplydate1().equals("")) {

            } else {
                data.setBeginTime(sdf.parse(data.getApplydate1()));
            }
            if (data.getApplydate2() == null || data.getApplydate2().equals("")) {

            } else {
                data.setEndTime(sdf.parse(data.getApplydate2()));
            }

            /* data.setStates(data.getStates() == null ? 1 : data.getStates()); */
            PaginationBean<BjxxClassWorkInstead> paginationBean = bjxxClassWorkInsteadService
                .queryPageList_admin(data, page);
            ResponseResult result = new ResponseResult();

            if (paginationBean.getPageList().isEmpty()) {
                result.setRows(new ArrayList<BjxxClassWorkInstead>());
            } else {
                result.setRows(paginationBean.getPageList());
            }
            result.setTotal(paginationBean.getTotalRecords());
            return result;

        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        return null;
    }

    @RequestMapping(value = "/details_admin", method = RequestMethod.GET)
    public String details_admin(ModelMap model, HttpServletRequest request,
                                @RequestParam(value = "id", required = false) Integer id,
                                @RequestParam(value = "gid", required = false) String gid) {
        try {

            BjxxClassWorkInstead data = new BjxxClassWorkInstead();
            data.setDbid(id);
            data.setGid(gid);
            data = bjxxClassWorkInsteadService.queryByPK(data);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            data.setKsrq1(sdf.format(data.getKsrq()));
            data.setJsrq1(sdf.format(data.getJsrq()));
            data.setApplydate1(sdf.format(data.getApplydate()));
            model.addAttribute("data", data);

            BjxxClassWorkInsteadAtt att = new BjxxClassWorkInsteadAtt();
            att.setParentid(id);
            List<BjxxClassWorkInsteadAtt> attList = bjxxClassWorkInsteadAttService.queryList(att);
            if (attList != null && !attList.isEmpty()) {
                model.addAttribute("List", attList);
                model.put("studentListJson", JSON.toJSONString(attList));
                model.put("logo", true);
            } else {
                java.util.List<Employee> list = commonService
                    .getqueryStudentClassByBzr(new Student());
                model.addAttribute("List", list);
                model.put("studentListJson", JSON.toJSONString(list));
                model.put("logo", false);
            }

            return "bjxxClassWorkInstead/detailsBjxxClassWorkInstead_admin";
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("查询 queryByPK [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(), e.getMessage());
            model.addAttribute(Constant.EXPTION_MSG_KEY, e);
            return "error/error";
        }
    }

    @SysLogger(description = "班主任顶班信息", moduleName = "班主任顶班管理", method = "审核通过")
    @RequestMapping(value = "/audit")
    public String audit(ModelMap model, HttpServletRequest request, BjxxClassWorkInstead data,
                        @RequestParam(value = "states", required = false) String states) {
        try {
            UserEmployee userEmployee = (UserEmployee) request.getSession()
                .getAttribute(Constant.LOGIN_CONSOLE_USER);

            String auditresult = data.getAuditresult();
            String dbxm = data.getDbxm();
            data = bjxxClassWorkInsteadService.queryByPK(data);
            data.setStates(Integer.valueOf(states));
            data.setAuditresult(auditresult);
            data.setAuditorid(userEmployee.getEmid() == null ? null : userEmployee.getEmid());
            data.setAuditorname(userEmployee.getXm());
            data.setAudittime(new Date());

            /*    String dbempids = request.getParameter("dbempids");*/
            /*  if (!dbempids.equals("")) {*/
            if (states.equals("2")) {
                String dbzgh = "";
                for (String dd : dbxm.split("、")) {
                    String[] strs = dd.split(",");
                    for (int i = 0; i < strs.length; i++) {
                        if (i == 0) {
                            data.setDbxm(strs[i].substring(1, strs[i].length()));
                        }
                        /*  if (i == 1) {
                          data.setDbzgh(strs[i]);
                          }*/
                        if (i == 1) {
                            dbzgh = (strs[i].substring(0, strs[i].length() - 1));
                        }
                    }
                }
                BjxxClassWorkInsteadAtt att = new BjxxClassWorkInsteadAtt();
                att.setParentid(data.getDbid());
                List<BjxxClassWorkInsteadAtt> attList = bjxxClassWorkInsteadAttService
                    .queryList(att);
                if (attList != null && !attList.isEmpty()) {
                    for (BjxxClassWorkInsteadAtt r : attList) {
                        if (r.getZgh().equals(dbzgh)) {
                            data.setDbxm(r.getXm());
                            data.setDbzgh(r.getZgh());
                            data.setDbempid(r.getEmpid());
                            break;
                        }
                    }
                } else {
                    java.util.List<Employee> list = commonService
                        .getqueryStudentClassByBzr(new Student());
                    for (Employee r : list) {
                        if (r.getZgh().equals(dbzgh)) {
                            data.setDbxm(r.getXm());
                            data.setDbzgh(r.getZgh());
                            data.setDbempid(r.getEmid());
                            break;
                        }
                    }
                }
                bjxxClassWorkInsteadService.update(data);
            } else {
                bjxxClassWorkInsteadService.update(data);
            }
            /*  }*/

            return "redirect:index_admin";
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("审核 update [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(), e.getMessage());
            model.addAttribute(Constant.EXPTION_MSG_KEY, e);
            return "error/error";
        }
    }

    @SysLogger(description = "班主任顶班信息", moduleName = "班主任顶班管理", method = "审核")
    @RequestMapping(value = "/auditList")
    public String auditList(ModelMap model, HttpServletRequest request) {
        try {
            UserEmployee userEmployee = (UserEmployee) request.getSession()
                .getAttribute(Constant.LOGIN_CONSOLE_USER);
            String[] idList = request.getParameter("idList").split(",");
            String states = request.getParameter("states1");

            BjxxClassWorkInstead data = new BjxxClassWorkInstead();
            for (String dbid : idList) {
                if (states.equals("2")) {
                    data.setDbid(Integer.valueOf(dbid));
                    data = bjxxClassWorkInsteadService.queryByPK(data);
                    data.setStates(Integer.valueOf(states));
                    data.setAuditorid(
                        userEmployee.getEmid() == null ? null : userEmployee.getEmid());
                    data.setAuditorname(userEmployee.getXm());
                    data.setAudittime(new Date());
                    data.setAuditresult(null);

                    BjxxClassWorkInsteadAtt att = new BjxxClassWorkInsteadAtt();
                    att.setParentid(Integer.valueOf(dbid));
                    List<BjxxClassWorkInsteadAtt> attList = bjxxClassWorkInsteadAttService
                        .queryList(att);
                    for (BjxxClassWorkInsteadAtt rr : attList) {
                        data.setDbempid(rr.getEmpid());
                        data.setDbzgh(rr.getZgh());
                        data.setDbxm(rr.getXm());
                        break;
                    }

                    bjxxClassWorkInsteadService.update(data);

                } else {
                    data.setDbid(Integer.valueOf(dbid));
                    data = bjxxClassWorkInsteadService.queryByPK(data);
                    data.setStates(Integer.valueOf(states));
                    data.setAuditorid(
                        userEmployee.getEmid() == null ? null : userEmployee.getEmid());
                    data.setAuditorname(userEmployee.getXm());
                    data.setAudittime(new Date());
                    data.setAuditresult(null);
                    bjxxClassWorkInsteadService.update(data);

                }
            }

            //bjxxClassWorkInsteadService.auditList(data);
            BaseController.setParameter("msg", "审核成功");
            return "redirect:index_admin";
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("审核 delete [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(), e.getMessage());
            model.addAttribute(Constant.EXPTION_MSG_KEY, e);
            return "error/error";
        }
    }

    /**
     * 下载
     * 
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/export_admin")
    public @ResponseBody String export_admin(HttpServletRequest request,
                                             HttpServletResponse response,
                                             BjxxClassWorkInstead data,
                                             @RequestParam("bodyData") String bodyData,
                                             ModelMap model) throws Exception {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            if (data.getApplydate1() == null || data.getApplydate1().equals("")) {

            } else {
                data.setBeginTime(sdf.parse(data.getApplydate1()));
            }
            if (data.getApplydate2() == null || data.getApplydate2().equals("")) {

            } else {
                data.setEndTime(sdf.parse(data.getApplydate2()));
            }

            BaseController.setSortByColumn(request, "applydate", "desc");
            data.setOrderField((String) request.getAttribute("orderField"));
            data.setOrderFlag((String) request.getAttribute("orderFlag"));
            List<BjxxClassWorkInstead> datalist = bjxxClassWorkInsteadService.export(data);

            /*   List<BjxxClassWorkInstead> datalist = JSONArray.parseArray(bodyData,
                BjxxClassWorkInstead.class);*/

            String[][] title_name = { { "申请人姓名", "申请人工号", "申请日期", "状态", "顶班人姓名", "开始日期", "结束日期" } };
            String[] title_style = { "hwzs_16_bold_25", "kt_12_bold_18", "st_12_r_20", "st_12_n_20",
                                     "st_12_n_20", "st_12_n_20", "st_12_n_20" };
            String fileName = "班主任顶班申请信息";
            String[] remark = { "" };
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            if (datalist != null && !datalist.isEmpty()) {
                for (BjxxClassWorkInstead r : datalist) {
                    Map<String, Object> map = new LinkedHashMap<String, Object>();
                    map.put("xm", r.getXm());
                    map.put("zgh", r.getZgh());
                    map.put("applydate", r.getApplydate());
                    if (r.getStates() == 1) {
                        map.put("states", "审核中");
                    } else if (r.getStates() == 0) {
                        map.put("states", "未提交");
                    } else if (r.getStates() == 2) {
                        map.put("states", "审核通过");
                    } else if (r.getStates() == 3) {
                        map.put("states", "审核退回");
                    }
                    map.put("dbxm", r.getDbxm());
                    map.put("ksrq", sdf.format(r.getKsrq()));
                    map.put("jsrq", sdf.format(r.getJsrq()));
                    list.add(map);
                }
            }
            HSSFWorkbook wb = ExpExcelUtil.getWorkBookExcel(title_name, title_style, 100, remark,
                fileName, list);
            try {
                String filenameConvert = "班主任顶班申请信息";/* new String(fileName.getBytes("GB2312"), "ISO8859-1"); */
                response.reset();
                response.setContentType("application/vnd.ms-excel;charset=utf-8");
                response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String((filenameConvert + ".xls").getBytes(),
                        "iso-8859-1"));
                response.setDateHeader("Expires", 0);
                ServletOutputStream os = response.getOutputStream();
                wb.write(os);
                os.flush();
            } catch (Exception e) {
                System.out.println("网络连接断开，可能用户取消了导出");
                model.addAttribute(Constant.EXPTION_MSG_KEY, e);
                return "error/error";
            }
        } catch (RPCException e) {
            LOGGER.error("获得信息 export[{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(), e.getMessage());
            model.addAttribute(Constant.EXPTION_MSG_KEY, e);
            return "error/error";
        }
        return null;
    }

    /**
     * 下载
     * 
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/export_bzr")
    public @ResponseBody String export_bzr(HttpServletRequest request, HttpServletResponse response,
                                           BjxxClassWorkInstead data,
                                           @RequestParam("bodyData") String bodyData,
                                           ModelMap model) throws Exception {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            UserEmployee userEmployee = (UserEmployee) request.getSession()
                .getAttribute(Constant.LOGIN_CONSOLE_USER);
            if (data.getApplydate1() == null || data.getApplydate1().equals("")) {

            } else {
                data.setBeginTime(sdf.parse(data.getApplydate1()));
            }
            if (data.getApplydate2() == null || data.getApplydate2().equals("")) {

            } else {
                data.setEndTime(sdf.parse(data.getApplydate2()));
            }
            data.setEmpid(userEmployee.getEmid());
            BaseController.setSortByColumn(request, "applydate", "desc");
            data.setOrderField((String) request.getAttribute("orderField"));
            data.setOrderFlag((String) request.getAttribute("orderFlag"));
            List<BjxxClassWorkInstead> datalist = bjxxClassWorkInsteadService.export_bzr(data);

            String[][] title_name = { { "申请日期", "状态", "顶班人", /*"顶班人工号",*/ "开始日期", "结束日期" } };
            String[] title_style = { "hwzs_16_bold_25", "kt_12_bold_18", "st_12_r_20", "st_12_n_20",
                                     "st_12_n_20", "st_12_n_20" };
            String fileName = "班主任顶班申请信息";
            String[] remark = { "" };
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            /*  List<BjxxClassWorkInstead> datalist = JSONArray.parseArray(bodyData,
                BjxxClassWorkInstead.class);*/
            if (datalist != null && !datalist.isEmpty()) {
                for (BjxxClassWorkInstead r : datalist) {
                    Map<String, Object> map = new LinkedHashMap<String, Object>();
                    map.put("applydate", sdf.format(r.getApplydate()));
                    if (r.getStates() == 1) {
                        map.put("states", "审核中");
                    } else if (r.getStates() == 0) {
                        map.put("states", "未提交");
                    } else if (r.getStates() == 2) {
                        map.put("states", "审核通过");
                    } else if (r.getStates() == 3) {
                        map.put("states", "审核退回");
                    }
                    map.put("dbxm", r.getDbxm());
                    /*  map.put("dbzgh", r.getDbzgh());*/
                    map.put("ksrq", sdf.format(r.getKsrq()));
                    map.put("jsrq", sdf.format(r.getJsrq()));

                    list.add(map);
                }
            }
            HSSFWorkbook wb = ExpExcelUtil.getWorkBookExcel(title_name, title_style, 100, remark,
                fileName, list);
            try {
                String filenameConvert = "班主任顶班申请信息";/* new String(fileName.getBytes("GB2312"), "ISO8859-1"); */
                response.reset();
                response.setContentType("application/vnd.ms-excel;charset=utf-8");
                response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String((filenameConvert + ".xls").getBytes(),
                        "iso-8859-1"));
                response.setDateHeader("Expires", 0);
                ServletOutputStream os = response.getOutputStream();
                wb.write(os);
                os.flush();
            } catch (Exception e) {
                System.out.println("网络连接断开，可能用户取消了导出");
                model.addAttribute(Constant.EXPTION_MSG_KEY, e);
                return "error/error";
            }
        } catch (RPCException e) {
            LOGGER.error("获得信息 export[{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(), e.getMessage());
            model.addAttribute(Constant.EXPTION_MSG_KEY, e);
            return "error/error";
        }
        return null;
    }

}
